<?php
require_once "db.php";

global $database;

function redis_queue_send($redis, $queue, $data, $delay = 0) {
    $queue_waiting = '{redis-queue}-waiting'; //1.0.5版本之前为redis-queue-waiting
    $queue_delay = '{redis-queue}-delayed';//1.0.5版本之前为redis-queue-delayed

    $now = time();
    $package_str = json_encode([
        'id'       => rand(),
        'time'     => $now,
        'delay'    => 0,
        'attempts' => 0,
        'queue'    => $queue,
        'data'     => $data
    ]);
    if ($delay) {
        return $redis->zAdd($queue_delay, $now + $delay, $package_str);
    }
    return $redis->lPush($queue_waiting.$queue, $package_str);
}
$redis = new Redis;
$redis->connect('127.0.0.1', 6379);
$redis->auth('harrypekey123');
$queue = '-pattern-add';

$items = $database->select("pattern", "*");

$count = 0;
foreach ($items as $item) {
    $count++;

    $properties = $database->select("pattern_property", "*", [
        'pid' => $item['pid']
    ]);

    $item['properties'] = $properties;

    redis_queue_send($redis, $queue, $item);
}
echo $count . "\n";


//$queue = '-pattern-update';
//$item = $database->get("pattern", "*", ['pid' => 999]);
//$properties = $database->select("pattern_property", "*", [
//    'pid' => $item['pid']
//]);
//$item['properties'] = $properties;
//redis_queue_send($redis, $queue, $item);



